package com.stx.jeepay.payment.controller.payorder;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.stx.jeepay.core.entity.PayOrderEntity;
import com.stx.jeepay.core.entity.PayWayEntity;
import com.stx.jeepay.core.enums.CS;
import com.stx.jeepay.core.expection.BizException;
import com.stx.jeepay.core.model.ApiRes;
import com.stx.jeepay.core.service.PayWayService;
import com.stx.jeepay.core.utils.JeepayKit;
import com.stx.jeepay.payment.rqrs.UnifiedOrderRS;
import com.stx.jeepay.payment.rqrs.payorder.UnifiedOrderRQ;
import com.stx.jeepay.payment.rqrs.payorder.payway.AutoBarOrderRQ;
import com.stx.jeepay.payment.service.ConfigContextQueryService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@Api("统一下单")
@Slf4j
@RestController
public class UnifiedOrderController extends AbstractPayOrderController {
    @Autowired
    private PayWayService payWayService;

    @Autowired
    private ConfigContextQueryService configContextQueryService;

    /**
     * 统一下单接口
     **/
    @PostMapping("/api/pay/unifiedOrder")
    public ApiRes unifiedOrder() {
        UnifiedOrderRQ rq = getRQByWithMchSign(UnifiedOrderRQ.class);

        UnifiedOrderRQ bizRQ = buildBizRQ(rq);

        //实现子类的res
        ApiRes apiRes = unifiedOrder(bizRQ.getWayCode(), bizRQ);
        if (apiRes.getData() == null) {
            return apiRes;
        }

        UnifiedOrderRS bizRes = (UnifiedOrderRS) apiRes.getData();

        //聚合接口，返回的参数
        UnifiedOrderRS res = new UnifiedOrderRS();
        BeanUtils.copyProperties(bizRes, res);

        //只有 订单生成（QR_CASHIER） || 支付中 || 支付成功返回该数据
        if (bizRes.getOrderState() != null && (bizRes.getOrderState() == PayOrderEntity.STATE_INIT
                || bizRes.getOrderState() == PayOrderEntity.STATE_ING || bizRes.getOrderState() == PayOrderEntity.STATE_SUCCESS)) {
            res.setPayDataType(bizRes.buildPayDataType());
            res.setPayData(bizRes.buildPayData());
        }

        return ApiRes.okWithSign(res, configContextQueryService.queryMchApp(rq.getMchNo(), rq.getAppId()).getAppSecret());
    }

    private UnifiedOrderRQ buildBizRQ(UnifiedOrderRQ rq) {
        //支付方式  比如： ali_bar
        String wayCode = rq.getWayCode();

        //jsapi 收银台聚合支付场景 (不校验是否存在payWayCode)
        if (CS.PAY_WAY_CODE.QR_CASHIER.equals(wayCode)) {
            return rq.buildBizRQ();
        }

        //如果是自动分类条码
        if (CS.PAY_WAY_CODE.AUTO_BAR.equals(wayCode)) {
            AutoBarOrderRQ bizRQ = (AutoBarOrderRQ) rq.buildBizRQ();
            wayCode = JeepayKit.getPayWayCodeByBarCode(bizRQ.getAuthCode());
            rq.setWayCode(wayCode.trim());
        }

        if (payWayService.count(new LambdaQueryWrapper<PayWayEntity>().eq(PayWayEntity::getWayCode, wayCode)) <= 0) {
            throw new BizException("不支持的支付方式");
        }

        //转换为 bizRQ
        return rq.buildBizRQ();
    }
}
